<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div>
      <!-- 单文件上传 -->
      <input id="file" type="file" multiple="multiple" />
      <!-- 多文件上传 -->
      <!-- <input id="file" type="file" multiple="multiple"/> -->
      <button onclick="uploadFile()">uploadFile</button>
    </div>

    <script>
      function uploadFile() {
        const fileNode = file.files[0]
        console.log(fileNode)

        const xhr = new XMLHttpRequest()
        console.log(fileNode instanceof Blob)

        xhr.open('POST', `http://127.0.0.1:8000/upload?name=${fileNode.name}`, true)
        xhr.onreadystatechange = function () {
          if (xhr.readyState == 4 && xhr.status == 200) {
            alert(xhr.responseText)
          }
        }

        // 上传进度
        xhr.upload.onprogress = function progress(e) {
          console.log(e)
        }

        xhr.onerror = function (err) {
          console.log(err)
        }

        xhr.send(fileNode)
      }
    </script>
  </body>
</html>
